<?php
define('ROOT', dirname(__DIR__));

class Xidea {

    private static $app;

    private function __construct()
    {
    }

    public static function app()
    {
        if (!self::$app instanceof self) {
            self::$app = new self();
        }

        return self::$app;
    }

    public function run($config)
    {
        foreach ($config as $key => $item) {
            if ($key == 'componets') {
                foreach ($item as $k => $params) {
                    $this->$k = $params['class'];
                }
            } else {
                $this->$key = $item;
            }
        }
    }

    public static function autoload($class)
    {
        $class = str_replace('\\', '/', $class);
        $classFile = ROOT . '/' . $class . '.php';
        require $classFile;
    }

    public function __get($com)
    {
        return $this->$com;
    }

    public function __set($key, $item)
    {
        if (strpos($item, '\\') !== false) {
            $classMap = require __DIR__ . './classmap.php';

            $this->$key = new $classMap[$key];
        } else {
            $this->$key = $item;
        }
    }
}

spl_autoload_register(array('Xidea', 'autoload'));